package com.mano.mall.service.gatewayhandler.impl;

import com.alibaba.fastjson.JSONObject;
import com.mano.mall.api.dto.gatewayhandler.input.GatewayHandlerInputDTO;
import com.mano.mall.api.dto.gatewayhandler.output.GatewayHandlerOutputDTO;
import com.mano.mall.api.gatewayhandler.GatewayHandlerService;
import com.mano.mall.common.base.BaseApiService;
import com.mano.mall.common.base.BaseResponse;
import com.mano.mall.common.constants.Constants;
import com.mano.mall.common.mapper.MapperUtils;
import com.mano.mall.common.utils.MyBeanUtils;
import com.mano.mall.service.mapper.GatewayHandlerMapper;
import com.mano.mall.service.mapper.entity.GatewayHandlerEntity;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: zj
 * @Description:
 * @Date: Created in 16:26 2020/11/12
 * @Modified By:
 */
@RestController
public class GatewayHandlerServiceImpl extends BaseApiService implements GatewayHandlerService {

    @Autowired
    private GatewayHandlerMapper gatewayHandlerMapper;

    @Override
    @Transactional
//    @HystrixCommand(fallbackMethod = "defaultCallBackMethod")
    public BaseResponse<JSONObject> addGWHandler(GatewayHandlerInputDTO gatewayHandlerInputDTO) {

        GatewayHandlerEntity gatewayHandlerEntity = MyBeanUtils.dtoToDo(gatewayHandlerInputDTO, GatewayHandlerEntity.class);

        BaseResponse<List<GatewayHandlerOutputDTO>> gwHandlerList = getGWHandlerList();
        List<GatewayHandlerOutputDTO> handlerList = (List<GatewayHandlerOutputDTO>) gwHandlerList.getData();
        if(null!=handlerList){
            gatewayHandlerEntity.setSequence(handlerList.size()+1);
        }else{
            gatewayHandlerEntity.setSequence(1);
        }

        gatewayHandlerMapper.insert(gatewayHandlerEntity);

        return setResultSuccess();
    }

    @Override
    @Transactional
//    @HystrixCommand(fallbackMethod = "defaultCallBackMethod")
    public BaseResponse<JSONObject> deleteGWHandler(Integer id) {
        gatewayHandlerMapper.delete(id);
        return setResultSuccess();
    }

    @Override
    @Transactional
//    @HystrixCommand(fallbackMethod = "defaultCallBackMethod")
    public BaseResponse<JSONObject> openGWHandler(Integer id) {
        GatewayHandlerEntity handlerEntity = new GatewayHandlerEntity();
        handlerEntity.setId(id);
        handlerEntity.setIsOpen(Constants.COMMON_STATUS_VAILD);
        gatewayHandlerMapper.updateStatus(handlerEntity);

        return setResultSuccess();
    }

    @Override
    @Transactional
//    @HystrixCommand(fallbackMethod = "defaultCallBackMethod")
    public BaseResponse<JSONObject> closeGWHandler(Integer id) {
        GatewayHandlerEntity handlerEntity = new GatewayHandlerEntity();
        handlerEntity.setId(id);
        handlerEntity.setIsOpen(Constants.COMMON_STATUS_INVALID);
        gatewayHandlerMapper.updateSequence(handlerEntity);

        return setResultSuccess();
    }

    @Override
    @Transactional
//    @HystrixCommand(fallbackMethod = "defaultCallBackMethod")
    public BaseResponse<JSONObject> upGWHandler(Integer id) {
        GatewayHandlerEntity gatewayHandlerEntity = gatewayHandlerMapper.getById(id);
        int current = gatewayHandlerEntity.getSequence();
        int pre = current-1;
        GatewayHandlerEntity preGatewayHandlerEntity = gatewayHandlerMapper.getBySequence(pre);

        gatewayHandlerEntity.setSequence(pre);
        gatewayHandlerMapper.updateSequence(gatewayHandlerEntity);

        preGatewayHandlerEntity.setSequence(current);
        gatewayHandlerMapper.updateSequence(preGatewayHandlerEntity);
        return setResultSuccess();
    }

    @Override
    @Transactional
//    @HystrixCommand(fallbackMethod = "defaultCallBackMethod")
    public BaseResponse<JSONObject> downGWHandler(Integer id) {
        GatewayHandlerEntity gatewayHandlerEntity = gatewayHandlerMapper.getById(id);
        int current = gatewayHandlerEntity.getSequence();
        int next = current + 1;
        GatewayHandlerEntity nextGatewayHandlerEntity = gatewayHandlerMapper.getBySequence(next);

        gatewayHandlerEntity.setSequence(next);
        gatewayHandlerMapper.updateSequence(gatewayHandlerEntity);

        nextGatewayHandlerEntity.setSequence(current);
        gatewayHandlerMapper.updateSequence(nextGatewayHandlerEntity);
        return setResultSuccess();
    }

    @Override
//    @HystrixCommand(fallbackMethod = "defaultCallBackMethod")
    public BaseResponse<List<GatewayHandlerOutputDTO>> getGWHandlerList() {
        List<GatewayHandlerEntity> list =  gatewayHandlerMapper.getHandlerList();
        List<GatewayHandlerOutputDTO> outputDTOS = MapperUtils.mapAsList(list,GatewayHandlerOutputDTO.class);
        return setResultSuccess(outputDTOS);
    }

}
